//https://www.bilibili.com/video/BV1d54y1f7B3/?spm_id_from=333.337.search-card.all.click&vd_source=d6fe65f095f01f05591e89d810720ccc
// 2024-10-20

import java.util.Arrays;

public class P019_合并两个有序数组_三指针法 {

    public static void main(String[] args) {
        int[] nums1 = {1,3,5,7,9,11};
        int[] nums2 = {2,4,6,8,10,12};
        System.out.println(Arrays.toString(fun(nums1, nums2)));
    }

    //num1和num2都升序
    private static int[] fun(int[] nums1,int[] nums2) {
        int[] nums3 = new int[nums1.length + nums2.length];
        int i = 0,j = 0,k = 0;
        while (i < nums1.length && j < nums2.length)
            //比较，谁小谁放
            nums3[k++] = nums1[i] <= nums2[j] ? nums1[i++] : nums2[j++];
        // 扫尾工作
        while (i < nums1.length) nums3[k++] = nums1[i++];
        while (j < nums2.length) nums3[k++] = nums2[j++];
        return nums3;
    }
}
